<template>
  <div class="table_continer">
    <div>
      <!-- 列表 ------------------------------------------------------------------------------------------------------->
      <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" row-key="storehouseId"
        default-expand-all :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        @selection-change="handleSelectionChange">
        <el-table-column prop="storehouseName" label="仓库名称" min-width="260" />
        <el-table-column prop="sort" label="排序" />
        <el-table-column prop="status" label="状态" min-width="160">
          <template slot-scope="scope">
            <dict-tag :options="statusFlag" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间" min-width="160">
          <template slot-scope="{row}">
            <div> {{ row.updateTime || '-' }} </div>
          </template>
        </el-table-column>
        <el-table-column prop="updateBy" label="更新人" min-width="160">
          <template slot-scope="{row}">
            <div> {{ row.updateBy || '-' }} </div>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" min-width="160">
          <template slot-scope="{row}">
            <div> {{ row.remark || '-' }} </div>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" min-width="160" />
        <el-table-column label="创建部门" align="center" min-width="180" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.deptName || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="创建人" align="center" min-width="180" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.createBy || '-' }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="220">
          <template slot-scope="scope">
            <!-- 此处ID字段特别注意 ------------------------------------------------------------------------------------>
            <el-button v-hasPermi="['inventory_right_update']" type="primary" size="small" icon="el-icon-edit"
              @click="handleEdit(scope.row.storehouseId)">编辑
            </el-button>
            <el-button v-hasPermi="['inventory_right_dis']" v-show="scope.row.status == 0" icon="el-icon-lock"
              type="danger" size="small" @click="handleAlter(scope.row.storehouseId, scope.row.status)">禁用</el-button>
            <el-button v-hasPermi="['inventory_right_dis']" v-show="scope.row.status == 1" icon="el-icon-unlock"
              type="success" size="small" @click="handleAlter(scope.row.storehouseId, scope.row.status)">启用</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- ------------------------------------------------------------------------------------------------------------>
    </div>
    <!-- --------------------------------分页器---------------------------------------------------------- -->
    <div class="table_continer_pagination">
      <el-pagination :current-page="pageParams.pageNum" background :page-sizes="[10, 20, 50, 100]"
        :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script>
/** 拉取列表远程方法 ---------------------------------------------------------------------------------------------------------- */
import { listSort, statusSort } from '@/api/deviceConsumable/inventory'
/** ------------------------------------------------------------------------------------------------------------------------- */
import { pageCount } from '@/utils/pagination'
/** 查询参数处理 ---------------------------------------------------------------------------------------------------------- */
function processQueryParams (queryParams, pageParams) {
  const params = Object.assign({}, queryParams, pageParams)
  return params
}
/** ------------------------------------------------------------------------------------------------------------------------- */

export default {
  name: 'PaginationTable',
  data () {
    return {
      pagination: {
        total: 1,
        pageCount: 1
      },
      pageParams: {
        pageNum: 1,
        pageSize: 10,
        /** 列表默认排序字段 ---------------------------------------------------------------------------------------------------------- */
        orderByColumn: 'parentId',
        /** ------------------------------------------------------------------------------------------------------------------------- */
        isAsc: 'desc'
      },
      queryParams: null,
      tableData: [],
      multipleSelection: []
    }
  },
  mounted () {
    this.refreshList()
  },
  methods: {
    refreshList () {
      const params = processQueryParams(this.queryParams, this.pageParams)
      console.log('查询参数', params);
      // --------consolelog('查询参数', params)
      listSort(params).then((res) => {
        console.log('获取列表', res);
        // --------consolelog('获取列表', res)
        this.pagination.total = res.total
        this.pagination.pageCount = pageCount(this.pagination.total, this.pageParams.pageSize)
        this.tableData = this.handleTree(res.data, "storehouseId");
      })
    },
    handleQuery (queryParams) {
      this.queryParams = queryParams
      this.refreshList()
    },
    handleSizeChange (value) {
      this.pageParams.pageSize = value
      this.refreshList()
    },
    handleCurrentChange (value) {
      this.pageParams.pageNum = value
      this.refreshList()
    },
    handleSelectionChange (value) {
      // console.log('handleSelectionChange', value)
    },
    /** 传递点击【编辑】事件 ---------------------------------------------------------------------------------------------------------- */
    handleEdit (id) {
      this.$emit('click_edit', id)
    },
    // 禁用----启用事件------------------------------------------------------------------------------------------------------------------
    handleAlter (storehouseId, status) {
      const rule = {
        storehouseId,
        status: status == 0 ? 1 : 0
      }
      statusSort(rule).then(() => {
        if (status == 0) {
          this.$message.error('禁用成功');
        } else {
          this.$message.success('启用成功');
        }
        this.refreshList()
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.table_continer {
  border-radius: 5px;

  &_pagination {
    background-color: white;
    padding: 10px;
    margin-bottom: 10px;
  }
}
</style>
